package comic.content.api;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONException;
import org.json.JSONObject;

import comic.content.po.ComicPO;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ComicApi {
	private static final String COMIC_RADOM_URL = "http://www.ecook.cn/joke/comicServlet";
	private static final String COMIC_BASE_URL = "http://www.ecook.cn/joke/comicByOrderServlet";
	private static final String COMIC_MAGIC_URL = "http://www.ecook.cn/joke/getComicMagicServlet";

	public Bitmap downloadFile(String imageid) {
		InputStream is = null;
		Bitmap bmImg;
		try {
			URL realUrl = new URL(COMIC_MAGIC_URL);
			// 打开和URL之间的连接
			System.setProperty("http.keepAlive", "false");
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			PrintWriter out = new PrintWriter(new OutputStreamWriter(conn
					.getOutputStream(), "utf-8"));
			// 发送请求参数
			out.print("id=" + imageid);
			// flush输出流的缓冲
			out.flush();
			conn.setReadTimeout(3000000);
			is = new BufferedInputStream(conn.getInputStream());
			bmImg = BitmapFactory.decodeStream(is);
			return bmImg;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public ComicPO getComicPo(String startId, int type) {
		ComicPO po = new ComicPO();
		InputStream is = openComicConn(startId, type);
		byte[] buf = new byte[1024];
		StringBuffer sb = new StringBuffer();
		int length;
		try {
			while ((length = is.read(buf)) != -1) {
				sb.append(new String(buf, 0, length));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			JSONObject jsonObject = new JSONObject(sb.toString());
			String id = jsonObject.getString("id");
			String name = jsonObject.getString("name");
			po.setId(id);
			po.setName(name);
			return po;
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	
	public ComicPO getRomComicPo() {
		ComicPO po = new ComicPO();
		InputStream is = openComicRodanConn();
		byte[] buf = new byte[1024];
		StringBuffer sb = new StringBuffer();
		int length;
		try {
			while ((length = is.read(buf)) != -1) {
				sb.append(new String(buf, 0, length));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			JSONObject jsonObject = new JSONObject(sb.toString());
			String id = jsonObject.getString("id");
			String name = jsonObject.getString("name");
			po.setId(id);
			po.setName(name);
			return po;
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public InputStream openComicConn(String startId, int type) {
		InputStream is = null;
		try {
			URL realUrl = new URL(COMIC_BASE_URL);
			// 打开和URL之间的连接
			System.setProperty("http.keepAlive", "false");
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			PrintWriter out = new PrintWriter(new OutputStreamWriter(conn
					.getOutputStream(), "utf-8"));
			// 发送请求参数
			if (type == 1) {
				out.print("id=" + startId);
			} else {
				out.print("id=" + startId + "&type=2");
			}
			// flush输出流的缓冲
			out.flush();
			is = conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return is;
	}
	
	public InputStream openComicRodanConn() {
		InputStream is = null;
		try {
			URL realUrl = new URL(COMIC_RADOM_URL);
			// 打开和URL之间的连接
			System.setProperty("http.keepAlive", "false");
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			PrintWriter out = new PrintWriter(new OutputStreamWriter(conn
					.getOutputStream(), "utf-8"));
			// flush输出流的缓冲
			out.flush();
			is = conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return is;
	}
}
